﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ZedGraph;

namespace ControlerLib
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class ZedGraphWPF : UserControl
    {
        ZedGraphControl zg;
        PointPairList ppl;
        RollingPointPairList rppl;

        public ZedGraphWPF()
        {
            InitializeComponent();
        }

        private void host_Loaded(object sender, RoutedEventArgs e)
        {
            zg = new ZedGraphControl();
            zg.Dock = System.Windows.Forms.DockStyle.Fill;
            //zg.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
            //zg.Anchor = System.Windows.Forms.AnchorStyles.Left;
            //zg.Anchor = System.Windows.Forms.AnchorStyles.Right;
            //zg.Anchor = System.Windows.Forms.AnchorStyles.Top;

            zg.GraphPane.XAxis.Scale.MaxAuto = true;
            zg.GraphPane.XAxis.Scale.MinAuto = true;
            zg.GraphPane.YAxis.Scale.MaxAuto = true;
            zg.GraphPane.YAxis.Scale.MinAuto = true;
            host.Child = zg;

            zg.MouseDoubleClick+=new System.Windows.Forms.MouseEventHandler(zg_MouseDoubleClick);

            //this.grid1.Children.Add(host);
        }

        public void SetTitles(string [] titles)
        {
            SetTitle(titles[0]);
            SetYAxisTitle(titles[1]);
            SetXAxisTitle(titles[2]);
        }

        public void SetTitle(string Title)
        {
            zg.GraphPane.Title.Text = Title;
        }

        public void SetXAxisTitle(string Title)
        {
            zg.GraphPane.XAxis.Title.Text = Title;
        }
        public void SetYAxisTitle(string Title)
        {
            zg.GraphPane.YAxis.Title.Text = Title;
        }

        public void UpDateRollingPlot(double dataY, int size)
        {
            if(rppl==null)
            {
                rppl= new RollingPointPairList(size);
                zg.GraphPane.AddCurve("", rppl, System.Drawing.Color.Blue, SymbolType.None);
            }
            if (rppl.Count == 0)
            {
                rppl.Add(0, dataY);
            }
            else
            {
                rppl.Add(rppl[rppl.Count-1].X+1, dataY);
            }
            Refresh();
        }

        public void ReconnectRollingPairList()
        {
            zg.GraphPane.AddCurve("", rppl, System.Drawing.Color.Blue, SymbolType.None);
        }

        public void CreatePlot(double[] dataY)
        {
            double[] dataX = new double[dataY.Length];
            for (int i = 0; i < dataY.Length; i++)
            {
                dataX[i] = i;
            }
            CreatePlot(dataX,dataY);
        }
        public void CreatePlot(double[] dataX, double[] dataY)
        {
                ppl = new PointPairList(dataX, dataY);
                zg.GraphPane.AddCurve("", ppl, System.Drawing.Color.Blue, SymbolType.None);
        }
        public void ClearAllCurves()
        {
            rppl = null;
            ppl = null;
            zg.GraphPane.CurveList.Clear();
            Refresh();
        }
        public void Refresh()
        {
            zg.AxisChange();
            zg.Invalidate();
        }

        public void SetMinMaxHorizonzal()
        {

        }

        public void CreateBarPlot(double[] dataY)
        {
            double[] dataX = new double[dataY.Length];
            for (int i = 0; i < dataY.Length; i++)
            {
                dataX[i] = i;
            }
            CreateBarPlot(dataX, dataY);
        }
        public void CreateBarPlot(double[] dataX, double[] dataY)
        {
            ClearAllCurves();
            zg.GraphPane.AddBar("",dataX,dataY,System.Drawing.Color.Blue);
        }



        private void zg_MouseDoubleClick(object sender, EventArgs e)
        {
            if (rppl != null)
            {
                double mean = 0;
                for (int i = 0; i < rppl.Count; i++)
                {
                    mean += rppl[i].Y;
                }
                mean = mean / rppl.Count;
                string str = mean.ToString("g",System.Globalization.CultureInfo.InvariantCulture.NumberFormat); 

                Clipboard.SetText(str);
            }
        }
    }
}
